package org.garbagecan.quartzstudy.cron;

import java.util.TimeZone;

import org.quartz.Calendar;
import org.quartz.impl.calendar.BaseCalendar;

public class RangeCalendar extends BaseCalendar {

	private TimeZone timeZone;
	private long startTimeStamp;
	private long endTimeStamp;

	public RangeCalendar() {
		this(null, 0, 0, null);
	}

	public RangeCalendar(long startTimeStamp, long endTimeStamp) {
		this(null, startTimeStamp, endTimeStamp, null);
	}

	public RangeCalendar(Calendar baseCalendar, long startTimeStamp,
			long endTimeStamp) {
		this(baseCalendar, startTimeStamp, endTimeStamp, null);
	}

	public RangeCalendar(long startTimeStamp, long endTimeStamp,
			TimeZone timeZone) {
		this(null, startTimeStamp, endTimeStamp, timeZone);
	}

	public RangeCalendar(Calendar baseCalendar, long startTimeStamp,
			long endTimeStamp, TimeZone timeZone) {
		super(baseCalendar);
		this.setTimeZone(timeZone);
		this.setStartTimeStamp(startTimeStamp);
		this.setEndTimeStamp(endTimeStamp);
	}

	public void setBaseCalendar(Calendar baseCalendar) {
		super.setBaseCalendar(baseCalendar);
	}

	public boolean isTimeIncluded(long timeStamp) {
		if ((getBaseCalendar() != null)
				&& (getBaseCalendar().isTimeIncluded(timeStamp) == false)) {
			return false;
		}

		if (timeStamp >= this.startTimeStamp && timeStamp <= this.endTimeStamp) {
			return false;
		}
		return true;
	}

	public long getNextIncludedTime(long timeStamp) {
		if (timeStamp >= this.startTimeStamp && timeStamp <= this.endTimeStamp) {
			return timeStamp;
		}
		return 0;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	public long getStartTimeStamp() {
		return startTimeStamp;
	}

	public void setStartTimeStamp(long startTimeStamp) {
		this.startTimeStamp = startTimeStamp;
	}

	public long getEndTimeStamp() {
		return endTimeStamp;
	}

	public void setEndTimeStamp(long endTimeStamp) {
		this.endTimeStamp = endTimeStamp;
	}
}
